package com.framework.service.common.impl;

import com.alibaba.fastjson.JSONObject;
import com.framework.common.model.response.R;
import com.framework.common.util.other.NumeralUtil;
import com.framework.common.util.redis.RedisKeyUtil;
import com.framework.mapper.common.DictMapper;
import com.framework.model.common.vo.DictVo;
import com.framework.model.system.SystemUser;
import com.framework.model.system.vo.SystemUserVo;
import com.framework.service.base.BaseService;
import com.framework.service.common.DictService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;

/**
 * @author 龘鵺
 * @version 1.0
 * @className com.framework.service.common.impl.DictServiceImpl
 * @description 字典业务接口实现类
 * @datetime 2024-04-20 14:57:12
 */
@Service("dictServiceImpl")
public class DictServiceImpl extends BaseService implements DictService {
    private Logger log = LoggerFactory.getLogger(DictServiceImpl.class);
    @Autowired
    private DictMapper dictMapper;

    /**
     * @param id 1 编号
     * @return com.framework.model.common.vo.DictVo
     * @title 根据编号查询字典
     * @description 根据编号查询字典
     * @author 龘鵺
     * @datetime 2024-04-20 14:57:12
     * @version 1.0
     */
    @Override
    public DictVo getByIdParam(Long id) {
        DictVo row = new DictVo();
        row.setId(id);
        row.setGtaeOperaterStatus(NumeralUtil.POSITIVE_ONE);
        DictVo p = dictMapper.selectByParam(row);
        return p;
    }

    /**
     * @param id 1 编号
     * @return com.framework.common.model.response.R
     * @title 根据编号查询字典
     * @description 根据编号查询字典
     * @author 龘鵺
     * @datetime 2024-04-20 14:57:12
     * @version 1.0
     */
    @Override
    public R<?> getByIdInfo(Long id) {
        DictVo row = new DictVo();
        row.setId(id);
        row.setGtaeOperaterStatus(NumeralUtil.POSITIVE_ONE);
        row = dictMapper.selectByParam(row);
        return R.ok(row);
    }

    /**
     * @param row 1 字典类
     * @return com.framework.common.model.response.R
     * @title 新增字典
     * @description 新增字典
     * @author 龘鵺
     * @datetime 2024-04-20 14:57:12
     * @version 1.0
     */
    @Override
    @Transactional
    public R<?> save(DictVo row) {
        int num = dictMapper.isExist(row);
        if (num > NumeralUtil.POSITIVE_ZERO) {
            return R.fail();
        }
        SystemUser systemUser = getUser();
        Long userId = systemUser.getId();
        Date date = new Date();
        row.setCreateId(userId);
        row.setCreateTime(date);
        row.setOperaterId(userId);
        row.setOperaterTime(date);
        row.setOperaterStatus(NumeralUtil.POSITIVE_ONE);
        dictMapper.insertSelective(row);
        DictVo redisRow = new DictVo();
        redisRow.setId(row.getId());
        redisRow.setDictKey(row.getDictKey());
        redisRow.setEnableStatus(row.getEnableStatus());
        if (row.getEnableStatus().intValue() == NumeralUtil.POSITIVE_ONE) {
            super.redisUtil.setString(RedisKeyUtil.getPermissionDictKey(row.getDictKey()), JSONObject.toJSONString(redisRow));
        }
        return R.ok();
    }

    /**
     * @param row 1 字典类
     * @return com.framework.common.model.response.R
     * @title 编辑字典
     * @description 编辑字典
     * @author 龘鵺
     * @datetime 2024-04-20 14:57:12
     * @version 1.0
     */
    @Override
    @Transactional
    public R<?> edit(DictVo row) {
        int num = dictMapper.isExist(row);
        if (num > NumeralUtil.POSITIVE_ZERO) {
            return R.fail();
        }
        DictVo queryParam = dictMapper.selectByPrimaryKey(row.getId());
        if (queryParam == null) {
            return R.fail("编辑信息不存在!");
        }
        SystemUser systemUser = getUser();
        Long userId = systemUser.getId();
        Date date = new Date();
        row.setOperaterTime(date);
        row.setOperaterId(userId);
        row.setOperaterStatus(NumeralUtil.POSITIVE_TWO);
        dictMapper.updateByPrimaryKeySelective(row);
        //判断字典键是否修改
        if (!queryParam.getDictKey().equals(row.getDictKey())) {
            //刷新字典键结束
            super.redisUtil.deleteKey(RedisKeyUtil.getPermissionDictKey(queryParam.getDictKey()));
        }
        if (row.getEnableStatus().intValue() == NumeralUtil.POSITIVE_ONE) {
            DictVo redisRow = new DictVo();
            redisRow.setId(row.getId());
            redisRow.setDictKey(row.getDictKey());
            redisRow.setEnableStatus(row.getEnableStatus());
            super.redisUtil.setString(RedisKeyUtil.getPermissionDictKey(row.getDictKey()), JSONObject.toJSONString(redisRow));
        } else {
            super.redisUtil.deleteKey(RedisKeyUtil.getPermissionDictKey(row.getDictKey()));
        }
        return R.ok();
    }

    /**
     * @param id 1 编号
     * @return com.framework.common.model.response.R
     * @title 删除字典
     * @description 删除字典
     * @author 龘鵺
     * @datetime 2024-04-20 14:57:12
     * @version 1.0
     */
    @Override
    @Transactional
    public R<?> delete(Long id) {
        DictVo queryParam = dictMapper.selectByPrimaryKey(id);
        if (queryParam == null) {
            return R.fail("删除不存在!");
        }
        DictVo row = new DictVo();
        SystemUser systemUser = getUser();
        Long userId = systemUser.getId();
        Date date = new Date();
        row.setId(id);
        row.setOperaterTime(date);
        row.setOperaterId(userId);
        row.setOperaterStatus(NumeralUtil.NEGATIVE_ONE);
        dictMapper.delete(row);
        super.redisUtil.deleteKey(RedisKeyUtil.getPermissionDictKey(queryParam.getDictKey()));
        return R.ok();
    }

    /**
     * @param idList 1 编号集合
     * @return com.framework.common.model.response.R
     * @title 批量删除字典
     * @description 批量删除字典
     * @author 龘鵺
     * @datetime 2024-04-20 14:57:12
     * @version 1.0
     */
    @Override
    @Transactional
    public R<?> batchDeleteList(List<Long> idList) {
        DictVo row = new DictVo();
        row.setIdList(idList);
        List<DictVo> dictList = dictMapper.findByList(row);
        if (dictList == null || dictList.size() == NumeralUtil.POSITIVE_ZERO) {
            return R.fail("请选择操作信息");
        }
        SystemUser systemUser = getUser();
        Date date = new Date();
        Long userId = systemUser.getId();
        String[] delKey = new String[dictList.size()];
        for (int i = NumeralUtil.POSITIVE_ZERO; i < dictList.size(); i++) {
            delKey[i] = RedisKeyUtil.getPermissionDictKey(dictList.get(i).getDictKey());
        }
        row.setOperaterStatus(NumeralUtil.NEGATIVE_ONE);
        row.setOperaterTime(date);
        dictMapper.deleteList(row);
        if (delKey != null && delKey.length > NumeralUtil.POSITIVE_ZERO) {
            super.redisUtil.deleteKey(delKey);
        }
        return R.ok();
    }

    /**
     * @param row 1 字典类
     * @return java.util.List<com.framework.model.common.vo.DictVo>
     * @title 查询字典集合
     * @description 查询字典集合
     * @author 龘鵺
     * @datetime 2024-04-20 14:57:12
     * @version 1.0
     */
    @Override
    public List<DictVo> findByList(DictVo row) {
        row.setGtaeOperaterStatus(NumeralUtil.POSITIVE_ONE);
        List<DictVo> list = dictMapper.findByList(row);
        return list;
    }

    /**
     * @param row 1 字典类
     * @return com.framework.common.model.response.R
     * @title 分页查询字典列表
     * @description 分页查询字典列表
     * @author 龘鵺
     * @datetime 2024-04-20 14:57:12
     * @version 1.0
     */
    @Override
    public R<?> findParamPageList(DictVo row) {
        row.setGtaeOperaterStatus(NumeralUtil.POSITIVE_ONE);
        int count = dictMapper.findPageListCount(row);
        if (count == NumeralUtil.POSITIVE_ZERO) {
            return R.ok();
        }
        List<DictVo> list = dictMapper.findPageList(row);
        return R.ok(list, count);
    }

    /**
     * @param row 1 字典类
     * @return java.util.List<com.framework.model.common.vo.DictVo>
     * @title 根据条件查询初始化集合
     * @description 根据条件查询初始化集合
     * @author 龘鵺
     * @datetime 2025-01-09 11:11:11
     */
    @Override
    public List<DictVo> findByInitList(DictVo row) {
        return dictMapper.findByInitList(row);
    }

    /**
     * @param id 1 编号
     * @return com.framework.common.model.response.R
     * @title 启用|禁用，角色
     * @description 启用|禁用，角色
     * @author 龘鵺
     * @datetime 2024-06-19 09:34:28
     * @version 1.0
     */
    @Override
    @Transactional
    public R<?> enable(Long id) {
        String timeKey = RedisKeyUtil.getObjKey("DICT_ENABLE_KEY", "DICT_ENABLE_KEY", id.toString());
        long expireNum = redisUtil.getExpire(timeKey);
        if (expireNum > NumeralUtil.POSITIVE_ZERO) {
            return R.fail("操作频繁,请" + expireNum + "秒后重试!");
        }
        DictVo row = new DictVo();
        SystemUserVo systemUserVo = getUser();
        Long userId = systemUserVo.getId();
        row.setId(id);
        row.setGtaeOperaterStatus(NumeralUtil.POSITIVE_ZERO);
        row = dictMapper.selectByRow(row);
        if (row == null) {
            return R.fail("信息不存在!");
        }
        redisUtil.setString(timeKey, NumeralUtil.STRING_POSITIVE_ZERO, NumeralUtil.POSITIVE_THIRTY);
        Date date = new Date();
        DictVo updateRow = new DictVo();
        updateRow.setId(id);
        updateRow.setOperaterId(userId);
        updateRow.setOperaterTime(date);
        updateRow.setOperaterStatus(NumeralUtil.POSITIVE_TWO);
        updateRow.setEnableStatus(row.getEnableStatus().intValue() == NumeralUtil.POSITIVE_TWO ? NumeralUtil.POSITIVE_ONE : NumeralUtil.POSITIVE_TWO);
        dictMapper.updateByPrimaryKeySelective(updateRow);
        if (updateRow.getEnableStatus().intValue() == NumeralUtil.POSITIVE_ONE) {
            row.setDescription(null);
            row.setEnableStatus(updateRow.getEnableStatus());
            super.redisUtil.setString(RedisKeyUtil.getPermissionDictKey(row.getDictKey()), JSONObject.toJSONString(row));
        } else {
            super.redisUtil.deleteKey(RedisKeyUtil.getPermissionDictKey(row.getDictKey()));
        }
        return R.ok();
    }
}